package com.cbgolf.oa.model;

import android.support.v4.util.ArrayMap;
import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.contract.ITeetimeContract;
import com.cbgolf.oa.entity.CodeTypeBean;
import com.cbgolf.oa.entity.NetResponse;
import com.cbgolf.oa.entity.TeeTimeBean;
import com.cbgolf.oa.entity.TeeTimeBean.TeeTimesBean;
import com.cbgolf.oa.event.Events;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.TypeUtil;
import com.cbgolf.oa.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.greenrobot.eventbus.EventBus;

/**
 * Author: lq
 * Created on 2018/6/4
 *
 * @author lq
 */
public class TeetimeModelImpl extends BaseModel implements ITeetimeContract.Model {


  private long mTime = DateUtil.getDayBegin().getTime();
  private String mAbCode;

  @Override
  public void requestAbCode() {
    Web.getOK(WebAPI.park_coures_select_get, new NetCallBack(response -> {
      sortCodeList(getList(response, CodeTypeBean.class));
    }));
  }

  private void sortCodeList(List<CodeTypeBean> listCode) {
    if (Util.listIsNull(listCode)) {
      backAbCodeResult(null);
      return;
    }
    ArrayList<String> list = new ArrayList<>();
    for (int i = 0; i < listCode.size(); i++) {
      list.add(listCode.get(i).codeType);
    }
    Collections.sort(list);
    mAbCode = list.get(0);
    backAbCodeResult(list);
    requestTeetime(mTime, mAbCode);
  }

  private void backAbCodeResult(ArrayList<String> list) {
    EventBus.getDefault().post(new Events.Builder()
        .msgTarget(DataManager.TEETIME_AB_CODE)
        .listString(list)
        .errorMsg(errorMsg)
        .errorCode(errorCode)
        .build());

  }

  @Override
  public void requestTeetime(long time, String abCode) {
    mTime = TypeUtil.zeroLong(time, mTime);
    mAbCode = TextUtil.Text(abCode, mAbCode);
    Map<String, String> map = new ArrayMap<>();
    map.put("teeTimeDate", TextUtil.Text(mTime));
    map.put("codeType", TextUtil.Text(mAbCode));
    Web.getOK(WebAPI.teetime_get,
        map, new NetCallBack(this::backTeetimeResult
        ));
  }


  private void backTeetimeResult(NetResponse response) {
    TeeTimeBean bean = getBean(response, TeeTimeBean.class);
    List<TeeTimesBean> list = null;
    if (null != bean) {
      list = bean.teeTimes;
    }
    DataUtil.setHasPermission(null != bean && bean.hasPermission);
    EventBus.getDefault().post(new Events.Builder()
        .msgTarget(DataManager.TEETIME_DATA)
        .listTeetime(list)
        .errorMsg(getErrorMsg(response))
        .errorCode(errorCode)
        .build());
  }

  @Override
  public void addPerson(TeeTimesBean bean) {
    Web.postOk(WebAPI.teetime_book_post + bean.teeTimeId, new NetCallBack(this::reduceAddResult));
  }

  @Override
  public void reducePerson(TeeTimesBean bean) {
    Web.deleteOk(WebAPI.teetime_book_delete + bean.teeTimeId,
        new NetCallBack(this::reduceAddResult));
  }

  private void reduceAddResult(NetResponse response) {
    if (NetUtils.SUCCESS == response.statusCode) {
      requestTeetime(0, null);
    } else {
      EventBus.getDefault().post(new Events.Builder()
          .msgTarget(DataManager.TEETIME_ADD_REDUCE_FAIL)
          .errorMsg(getErrorMsg(response))
          .errorCode(errorCode)
          .build());
    }
  }

}